﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThanhTu.GNC.Media.KQXS.DataAccess.DO;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using ThanhTu.GNC.Media.KQXS.Common;

namespace ThanhTu.GNC.Media.KQXS.DataAccess.DAL
{
    public class RegionDAL : BaseDAL
    {
        public RegionDAL() : base() { }
        public int SaveRegion(RegionDO vRegionD0)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "REGION_PKG.SAVEREGION";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("INID_REGION", OracleDbType.Varchar2);
                oraParam.Value = vRegionD0.RegionID;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("INREGION_NAME", OracleDbType.Varchar2);
                oraParam.Value = vRegionD0.RegionName;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                int result = ExecuteNoneQuery(oraComm);

                if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
                { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

                oraComm.Dispose();
                oraParam = null;

                return result;
            }
        }
        public int delete(string ID_Region)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "REGION_PKG.DELETEREGION";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("INID_REGION", OracleDbType.Varchar2);
                oraParam.Value = ID_Region;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.Int32);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                int result = ExecuteNoneQuery(oraComm);

                if (!Convert.IsDBNull(oraComm.Parameters["OUTRETURN"]))
                { result = Convert.ToInt32(oraComm.Parameters["OUTRETURN"].Value.ToString()); }

                oraComm.Dispose();
                oraParam = null;

                return result;
            }
        }
        public RegionDO getbyRegion(string IDRegion)
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "REGION_PKG.GETBYREGIONPK";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("INID_REGION", OracleDbType.Varchar2);
                oraParam.Value = IDRegion;
                oraComm.Parameters.Add(oraParam);

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataRow dr = null;
                    RegionDO vRegionD0 = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            if (ds.Tables[0].Rows.Count > 0)
                            {
                                dr = ds.Tables[0].Rows[0];
                                vRegionD0 = new RegionDO();

                                if (!Convert.IsDBNull(dr["REGIONID"]))
                                { vRegionD0.RegionID = Convert.ToString(dr["REGIONID"]); }

                                if (!Convert.IsDBNull(dr["REGIONNAME"]))
                                { vRegionD0.RegionID = Utils.ToUTF8String((byte[])dr["REGIONNAME"]); }
                                dr = null;
                            }
                        }

                        ds.Dispose();
                    }

                    oraComm.Dispose();
                    oraParam = null;

                    return vRegionD0;
                }
            }
        }
        public List<RegionDO> SelectALL()
        {
            using (OracleCommand oraComm = new OracleCommand())
            {
                oraComm.CommandType = CommandType.StoredProcedure;
                oraComm.CommandText = "Region_PKG.GetAllRegionNonePage";
                OracleParameter oraParam = null;

                oraParam = new OracleParameter("OUTRETURN", OracleDbType.RefCursor);
                oraParam.Direction = ParameterDirection.Output;
                oraComm.Parameters.Add(oraParam);

                using (DataSet ds = GetDataSet(oraComm))
                {
                    DataTable dt = null;
                    List<RegionDO> vRegionD0Collection = null;
                    DataRow dr = null;
                    RegionDO vRegionD0 = null;

                    if (ds != null)
                    {
                        if (ds.Tables.Count > 0)
                        {
                            dt = ds.Tables[0];
                            vRegionD0Collection = new List<RegionDO>();

                            for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                dr = dt.Rows[i];
                                vRegionD0 = new RegionDO();

                                if (!Convert.IsDBNull(dr["REGIONID"]))
                                { vRegionD0.RegionID = Convert.ToString(dr["REGIONID"]); }

                                if (!Convert.IsDBNull(dr["REGIONNAME"]))
                                { vRegionD0.RegionName = Utils.ToUTF8String((byte[])dr["REGIONNAME"]); }

                                vRegionD0Collection.Add(vRegionD0);
                                dr = null;
                                vRegionD0 = null;
                            }
                        }
                    }
                    return vRegionD0Collection;
                }
            }
        }
    }
}
